<?php
class Default_Model_Image {
	protected $_messageError = null;
	protected $_imgPath = 'public/default/upload/place/photo/';
	protected   $_imgThumbPath = 'public/default/upload/place/thumb/';
	protected $_imgName = null;

	public function uploadImg() {
		try {
			$upload = new Zend_File_Transfer_Adapter_Http();
			$info = $upload->getFileName('picture');
			//preg_match('#\.([^\.]+)$#',$info,$matches);
			//$fileExtension  = $matches[1];

			$fileExtension = substr(strrchr($info, '.'), 1);

			$this->_imgName = 'pic_' . date('Ymd').'_' .time() . '.' . $fileExtension;

			// set img path
			$imgPath = $this->_imgPath . $this->_imgName;

			$options = array('target' => $imgPath,'overwrite'=>true);
			$upload->addFilter('Rename',$options,'picture');
			$upload->receive('picture');
				
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}

	public function createThumbImg() {
		try {
			$thumb = VnSpot_PhpThumb_PhpThumbFactory::create($this->_imgPath.$this->_imgName);
			$thumb->resize(200,200);
			$thumb->save($this->_imgThumbPath.$this->_imgName);
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
		}
	}

	public function deleteImg($img){
		$imgPath = $this->_imgPath.$img;
		$imgThumbPath = $this->_imgThumbPath.$img;
		try {
			if(file_exists($imgPath)){
				unlink($imgPath);
			}
			if(file_exists($imgThumbPath)){
				unlink($imgThumbPath);
			}
			return true;
		} catch (Exception $e) {
			return false;
			$this->_messageError = $e->getMessage();
		}
	}

	public function getImgName() {
		return $this->_imgName;
	}
	
	public function getImgPath() {
		return $this->_imgPath;
	}
	
	public function getImgThumbPath() {
		return $this->_imgThumbPath;
	}

	public function getError(){
		return $this->_messageError;
	}
}